package com.cz.service;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/*回读失败如下：
<?xml version="1.0" encoding="UTF-8"?>
<QDELITS type="ITS">
<ITS id=" " Port="9999"><!-- id 为设备终端 IP, Port 为通讯端口，一般是 9999 -->
<CMD  type="LED_PROG_READCURPLAY" id="-1"/><!-- id=-1代表回读失败-->
</ITS>
</QDELITS>*/
/*
回读成功如下： 1、文本节目区域：
<?xml version="1.0" encoding="UTF-8"?>
<QDELITS type="ITS">
<ITS id="5201000000101110" Port="9999">
<CMD  type=" LED_PROG_READCURPLAY " id="1"/><!-- id=1代表成功而且附带返回节目信息-->
<PROG id="0">
<AREA id="0" trad="0,0" wh="200,100">
<ITEM type="1" interval="5"><!--  type类型 单行文本 1   多行文本2  图片3-->
<text color="1" size="32" style="1"  font="宋体">谨慎驾驶！</text>
</ITEM>
</AREA >
</PROG>
</ITS>
</QDELITS>*/
public class receiveInfo {
    public  static Map<String,Socket> map = new HashMap<String,Socket>();
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(50100);
        System.out.println("-----正在监听50100端口---");
        Socket socket;
        while (true){
            socket = server.accept();
            System.out.println(socket);
            map.put("1",socket);
            InputStream is = socket.getInputStream();
            // 前8个字节
            byte[] b = new byte[8];
            is.read(b);
            int len = Integer.parseInt(new String(b, "UTF-8"));
            // 用来填充xml
            b = new byte[len];
            is.read(b);

            // 关闭资源
            is.close();
            socket.close();
            //server.close();

            String result = new String(b, "UTF-8");
            System.out.println(result);
        }

    }
}
